package com.goodmr.passport.webapp.controller;

import com.goodmr.passport.annotation.AuthUserId;
import com.goodmr.passport.webapp.Serivce.PasswordService;
import com.goodmr.passport.webapp.model.request.ResetPasswordReq;
import com.goodmr.passport.webapp.model.request.SetPasswordReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 忘记密码，重置帐号密码
 *
 * @author <a href="mailto:bingdong13@msn.com">bigo</a>
 */
@RestController
@RequestMapping(path = "/passport/password")
public class PasswordController {

    @Autowired
    @Qualifier("resetPasswordService")
    PasswordService resetPasswordService;

    @Autowired
    @Qualifier("setPasswordService")
    PasswordService setPasswordService;

    /**
     * 设置/修改密码
     *
     * @param userId 用户ID
     * @param body 请求数据包
     * @return 修改成功，返回no content，否则返回异常
     */
    @PostMapping("")
    public ResponseEntity set(@AuthUserId Long userId,
                              @Valid @RequestBody SetPasswordReq body) {

        body.setAccountId(userId);

        setPasswordService.modify(body);

        return ResponseEntity.noContent().build();
    }

    /**
     * 密码重置
     * 忘记帐号密码时，重新设置新密码
     *
     * @param body 请求数据包
     * @return 重置成功，返回no content，否则返回异常
     */
    @PutMapping("")
    public ResponseEntity reset(@Valid @RequestBody ResetPasswordReq body) {

        resetPasswordService.modify(body);

        return ResponseEntity.noContent().build();
    }
}
